package com.ruoyi.system.controller;

import java.security.Security;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.mapper.SysGoodsMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GoodsCategory;
import com.ruoyi.system.service.IGoodsCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Api("商品分类")
@RestController
@RequestMapping("/system/category")
public class GoodsCategoryController extends BaseController
{
    @Autowired
    private IGoodsCategoryService goodsCategoryService;
    @Autowired
    private SysGoodsMapper sysGoodsMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询商品分类列表
     */
    @ApiOperation("查询商品分类列表")
    @GetMapping("/list")
    public TableDataInfo list(GoodsCategory goodsCategory)
    {
        startPage();
        List<GoodsCategory> list = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
        for (GoodsCategory item : list) {
            SysGoods sysGoods = new SysGoods();
            sysGoods.setCategoryId(item.getId());
            List<SysGoods> goods = sysGoodsMapper.selectSysGoodsList(sysGoods);
            item.setGoodsNum(ObjectUtils.isEmpty(goods)? "0": goods.size() + "");
            if (!ObjectUtils.isEmpty(item.getUpdateBy())) {
                Long aLong = Long.parseLong(item.getUpdateBy());
                SysUser sysUser = sysUserMapper.selectUserById(aLong);
                item.setUpdateByName(sysUser.getUserName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @ApiOperation("导出商品分类列表")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsCategory goodsCategory)
    {
        List<GoodsCategory> list = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
        ExcelUtil<GoodsCategory> util = new ExcelUtil<GoodsCategory>(GoodsCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @ApiOperation("获取商品分类详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(goodsCategoryService.selectGoodsCategoryById(id));
    }

    /**
     * 新增商品分类
     */
    @ApiOperation("新增商品分类")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsCategory goodsCategory)
    {
        return toAjax(goodsCategoryService.insertGoodsCategory(goodsCategory));
    }

    /**
     * 修改商品分类
     */
    @ApiOperation("修改商品分类")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsCategory goodsCategory)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        goodsCategory.setUpdateBy(user.getUserId() + "");
        return toAjax(goodsCategoryService.updateGoodsCategory(goodsCategory));
    }

    /**
     * 删除商品分类
     */
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除商品分类")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(goodsCategoryService.deleteGoodsCategoryByIds(ids));
    }
}
